package com.kgcx.common.utils;


import org.apache.commons.lang.StringUtils;

import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;

/**
 * Created by Administrator on 2017/3/8.
 */
public class CollectionUtils extends org.apache.commons.collections.CollectionUtils {
    public static <T> T getFirst(List<T> list) {
        if (isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    public static String getAllString(Collection collection, String split) {
        if (isNotEmpty(collection)) {
            return StringUtils.join(collection, split);
        }
        return null;
    }

    public static boolean isEmptyNull(Collection collection) {
        return (null == collection || collection.stream().allMatch(i -> null == i));
    }

    public static <T> T runOnObjectOrCollection(T data, Consumer action) {
        if (null != data) {
            if (data instanceof Collection) {
                Collection<?> collection = (Collection) data;
                collection.forEach(i -> runOnObjectOrCollection(i, action));
            } else {
                action.accept(data);
            }
        }
        return data;
    }
}
